﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Xml;
//using System.CodeDom;
//using System.IO;
//using System.CodeDom.Compiler;
//using Microsoft.VisualStudio.TextTemplating.VSHost;

//namespace Dynamic.Testing.CodeDom
//{
//    public class MessageCodeGenerator : BaseCodeGeneratorWithSite
//    {
//        public CodeCompileUnit BuildCodeObject(XmlDocument messages)
//        {
//            var codeObject = new CodeCompileUnit();
//            var codeNamespace = new CodeNamespace("Dynamic.Testing.CodeDom");
//            codeObject.Namespaces.Add(codeNamespace);
//            var codeType = new CodeTypeDeclaration("Messages");
//            codeNamespace.Types.Add(codeType);
//            GenerateCatetoryClasses(codeType, messages);
//            return codeObject;
//        }

//        private void GenerateCatetoryClasses(CodeTypeDeclaration root, XmlDocument messageDoc)
//        {
//            var messageEntries = messageDoc.GetElementsByTagName("message").Cast<XmlElement>();
//            var categories = (from element in messageEntries
//                              select element.Attributes["category"].Value).Distinct();

//            foreach (var category in categories)
//            {
//                var categoryType = new CodeTypeDeclaration(category);
//                root.Members.Add(categoryType);

//                foreach (var element in messageDoc.GetElementsByTagName("message").Cast<XmlElement>().
//                    Where(element => element.Attributes["category"].Value == category))
//                {
//                    GenerateMessageProperty(element, categoryType);
//                }
//            }
//        }

//        private void GenerateMessageProperty(XmlElement messageEntry, CodeTypeDeclaration type)
//        {
//            string id = messageEntry.Attributes["id"].Value;
//            string value = messageEntry.Attributes["value"].Value;
//            string categotry = messageEntry.Attributes["category"].Value;

//            var field = new CodeMemberField(typeof(MessageEntry), id);
//            type.Members.Add(field);
//            field.Attributes = MemberAttributes.Public | MemberAttributes.Static;
//            field.InitExpression = new CodeObjectCreateExpression(
//                typeof(MessageEntry),
//                new CodePrimitiveExpression(id),
//                new CodePrimitiveExpression(value),
//                new CodePrimitiveExpression(categotry));
//        }

//        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
//        {
//            var messageDoc = new XmlDocument();
//            messageDoc.LoadXml(inputFileContent);
//            var codeObject = BuildCodeObject(messageDoc);
//            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
//            CodeGeneratorOptions options = new CodeGeneratorOptions();
//            options.BracingStyle = "C";
//            using (StringWriter writer = new StringWriter())
//            {
//                provider.GenerateCodeFromCompileUnit(codeObject, writer, options);
//                string code = writer.ToString();
//                byte[] preambleBytes = Encoding.Unicode.GetPreamble();
//                byte[] codeBytes = Encoding.Unicode.GetBytes(code);
//                byte[] result = new byte[preambleBytes.Length + codeBytes.Length];
//                Buffer.BlockCopy(preambleBytes, 0, result, 0, preambleBytes.Length);
//                Buffer.BlockCopy(codeBytes, 0, result, preambleBytes.Length, codeBytes.Length);

//                return result;
//            }

//        }

//        public override string GetDefaultExtension()
//        {
//            return ".cs";
//        }
//    }
//}
